﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Contacts;
using Google.GData.Extensions;

namespace GoogleContactMerge
{
    internal class DetailModel
    {
        public DetailModel(ContactEntry contactEntry)
        {
            this.ContactEntry = contactEntry;

            this.Name = contactEntry.Title.Text;
            foreach (Google.GData.Extensions.PhoneNumber phone in contactEntry.Phonenumbers)
            {
                var number = phone.Value;
                if (phone.Work)
                {
                    this.PhoneWork = number;
                    if (phone.Primary)
                        this.PhonePrimaryKind = PhoneKind.Work;
                }
                if (phone.Home)
                {
                    this.PhoneHome = number;
                    if (phone.Primary)
                        this.PhonePrimaryKind = PhoneKind.Home;
                }
                if (phone.Other)
                {
                    this.PhoneOther = number;
                    if (phone.Primary)
                        this.PhonePrimaryKind = PhoneKind.Other;
                }
            }
            foreach (var email in contactEntry.Emails)
            {
                var address = email.Address;
                if (email.Work)
                {
                    this.EmailWork = address;
                    if (email.Primary)
                        this.EmailPrimaryKind = EmailKind.Work;
                }
                if (email.Home)
                {
                    this.EmailHome = address;
                    if (email.Primary)
                        this.EmailPrimaryKind = EmailKind.Home;
                }
                if (email.Other)
                {
                    this.EmailOther = address;
                    if (email.Primary)
                        this.EmailPrimaryKind = EmailKind.Other;
                }
            }
        }

        public ContactEntry ContactEntry {get;private set;}

        public string Name{get;set;}
        public string EmailAddress { get; set; }
        public string PhoneHome { get; set; }
        public string PhoneWork { get; set; }
        public string PhoneOther { get; set; }
        public string EmailHome { get; set; }
        public string EmailWork { get; set; }
        public string EmailOther { get; set; }
        public EmailKind EmailPrimaryKind { get; set; }
        public PhoneKind PhonePrimaryKind { get; set; }

        public bool Delete { get; set; }
        bool _merge;
        public bool Merge { get { return _merge; } set{this._merge = value;} }

        public enum EmailKind { Work, Home, Other }
        public enum PhoneKind { Work, Home, Other }

        void SetPhone(PhoneKind kind, string phoneNumber)
        {
             bool found = false;

            if (kind == PhoneKind.Home)
            {
                foreach (Google.GData.Extensions.PhoneNumber phone in this.ContactEntry.Phonenumbers)
                {
                    if (phone.Home)
                    {
                        found = true;
                        phone.Value = phoneNumber;
                    }
                }
                if (!found)
                {
                    var newNumber = new Google.GData.Extensions.PhoneNumber(phoneNumber);
                    newNumber.Value = phoneNumber;
                    newNumber.Rel = ContactsRelationships.IsHome;
                    this.ContactEntry.Phonenumbers.Add(newNumber);
                }
            }
            else if(kind == PhoneKind.Work)
            {
                foreach (Google.GData.Extensions.PhoneNumber phone in this.ContactEntry.Phonenumbers)
                {
                    if (phone.Work)
                    {
                        found = true;
                        phone.Value = phoneNumber;
                    }
                }
                if (!found)
                {
                    var newNumber = new Google.GData.Extensions.PhoneNumber(phoneNumber);
                    newNumber.Value = phoneNumber;
                    newNumber.Rel = ContactsRelationships.IsWork;
                    this.ContactEntry.Phonenumbers.Add(newNumber);
                }
            }
            else if (kind == PhoneKind.Other)
            {
                foreach (Google.GData.Extensions.PhoneNumber phone in this.ContactEntry.Phonenumbers)
                {
                    if (phone.Other)
                    {
                        found = true;
                        phone.Value = phoneNumber;
                    }
                }
                if (!found)
                {
                    var newNumber = new Google.GData.Extensions.PhoneNumber(phoneNumber);
                    newNumber.Value = phoneNumber;
                    newNumber.Rel = ContactsRelationships.IsOther;
                    this.ContactEntry.Phonenumbers.Add(newNumber);
                }
            }
        }

        void SetEmail(EmailKind kind, string emailAddress)
        {
            bool found = false;

            if (kind == EmailKind.Home)
            {
                foreach (Google.GData.Extensions.EMail email in this.ContactEntry.Emails)
                {
                    if (email.Home)
                    {
                        found = true;
                        email.Value = emailAddress;
                    }
                }
                if (!found)
                {
                    var newEmail = new Google.GData.Extensions.EMail(emailAddress);
                    newEmail.Value = emailAddress;
                    newEmail.Rel = ContactsRelationships.IsHome;
                    this.ContactEntry.Emails.Add(newEmail);
                }
            }
            else if (kind == EmailKind.Work)
            {
                foreach (Google.GData.Extensions.EMail email in this.ContactEntry.Emails)
                {
                    if (email.Work)
                    {
                        found = true;
                        email.Value = emailAddress;
                    }
                }
                if (!found)
                {
                    var newEmail = new Google.GData.Extensions.EMail(emailAddress);
                    newEmail.Value = emailAddress;
                    newEmail.Rel = ContactsRelationships.IsWork;
                    this.ContactEntry.Emails.Add(newEmail);
                }

            }
            else if (kind == EmailKind.Other)
            {
                foreach (Google.GData.Extensions.EMail email in this.ContactEntry.Emails)
                {
                    if (email.Other)
                    {
                        found = true;
                        email.Value = emailAddress;
                    }
                }
                if (!found)
                {
                    var newEmail = new Google.GData.Extensions.EMail(emailAddress);
                    newEmail.Value = emailAddress;
                    newEmail.Rel = ContactsRelationships.IsOther;
                    this.ContactEntry.Emails.Add(newEmail);
                }

            }

        }

        
        public void WriteToContactEntry()
        {
            this.ContactEntry.Title.Text = this.Name;

            if(this.PhoneHome != null)
                this.SetPhone(PhoneKind.Home, this.PhoneHome);
                        
            if(this.PhoneWork != null)
                this.SetPhone(PhoneKind.Work, this.PhoneWork);

            if (this.PhoneOther != null)
                this.SetPhone(PhoneKind.Other, this.PhoneOther);

            if (this.EmailHome != null)
                this.SetEmail(EmailKind.Home, this.EmailHome);

            if (this.EmailWork != null)
                this.SetEmail(EmailKind.Work, this.EmailWork);

            if (this.EmailOther != null)
                this.SetEmail(EmailKind.Other, this.EmailOther);
        }
    }
}
